------------------------------------------------------------------------------------------------------------------------------------
      name:  plog_2
       log:  /Users/au660280/Dropbox/m5S/JoP_PlaceBasedCampaigning_replication/code/do/05_prepare_regional.log
  log type:  text
 opened on:  19 Sep 2022, 11:38:38

. *This code re-codes the comune level data and merges it with the Meetup info 
. /*
> Procedure: 
> The data comes in wide format. Thus:
> 1) We code the pre-treatment covariates for all respondents 
> 2) We keep only the pre-treatment covariates and the DVs 
> 3) We re-shape the file into long format dropping pre-treatment covariates into single variables, 
> while all DVs remain 2 variables (pre post). 
> 4) We can then subtract pre post Dvs such that we can estimate the change in DiD designs. 
> */
. 
. * directory definitions 
. 
.         project, doinfo
project PlaceBased_analysis > Project Name: PlaceBased_analysis
project PlaceBased_analysis > Project Dir.: /Users/au660280/Dropbox/m5S/JoP_PlaceBasedCampaigning_replication
project PlaceBased_analysis > Build start : 19sep2022, 11:38:37
project PlaceBased_analysis > Do-file Name: 05_prepare_regional.do
project PlaceBased_analysis > Enclosing do-files:
project PlaceBased_analysis >     PlaceBased_analysis.do

.         global pdir "`r(pdir)'"                                                 // the project's main dir.

.         global dofile "`r(dofile)'"                                             // do-file's stub name

.         global data_original = "$pdir/data_original"  //data directory for coded data 

.         global data_coded = "$pdir/data_coded"  //data directory for coded data 

.         global figures = "$pdir/results/figures"  //data directory for figures

.         global tables = "$pdir/results/tables"  //data directory for tables

. 
. 
. *********************************************
. ******comune level election results**********
. *********************************************
. /*
> Here we open the election data file:
> This data contains much more information then we need, 
> e.g. also prior election results. 
> 
> Since we are only interested in the changes between 
> 2018 & 2013 we only keep this information and delete
> the remaining year:
> */
.         project, original("$data_original/elections/elections.dta")
project PlaceBased_analysis > do-file uses original: "data_original/elections/elections.dta" filesig(3517279438:137311678)

.         use "$data_original/elections/elections.dta", clear 

. 
. * keep only years we need: 
. 
.         keep if year==2013 | year==2018
(97,121 observations deleted)

. 
. * do some cleaning of data:     
.         rename istatcode comune_id

.         sort comune_id year 

.         bysort comune_id: carryforward regione, replace 
regione:  (7,773 real changes made)

.         bysort comune_id: carryforward provincia, replace 
provincia:  (7,749 real changes made)

.         gsort comune_id -year 

.         bysort comune_id: carryforward regione, replace 
regione:  (0 real changes made)

.         bysort comune_id: carryforward provincia, replace 
provincia:  (0 real changes made)

. 
.         rename FORZAITALIA_* forza_*

.         rename LEGA_* lega_*

.         rename MOVIMENTO5STELLE_* m5stelle_*

.         rename PARTITODEMOCRATICO_* pd_* 

.         rename ILPOPOLODELLALIBERTA_* pdl_*

. 
. *some parties changed their names but are the same:
.         replace m5stelle_C=MOVIMENTO5STELLEBEPPEGRILLOI_C if year==2013
(8,092 real changes made)

.         replace m5stelle_S=MOVIMENTO5STELLEBEPPEGRILLOI_S if year==2013
(8,092 real changes made)

.         replace pd_C=VALLEEDAOSTE_C if provincia=="Aosta"
(74 real changes made)

.         replace pd_S=VALLEEDAOSTE_S if provincia=="Aosta"
(74 real changes made)

.         replace lega_S=LEGANORD_S if year==2013 
(7,759 real changes made)

.         replace lega_C=LEGANORD_C if year==2013 
(8,092 real changes made)

. 
. * code turnout from voters + registered voters:
.         gen turnout=(Numerovotanti/Numeroelettori)*100
(62 missing values generated)

. 
. 
. * code % votes in 100:  
.         foreach var of varlist forza_* lega_* m5stelle_* pd_* pdl_* {
  2.                 gen `var'_per=(`var'/Numeroelettori)*100
  3.         }
(8,228 missing values generated)
(8,228 missing values generated)
(395 missing values generated)
(62 missing values generated)
(62 missing values generated)
(62 missing values generated)
(371 missing values generated)
(136 missing values generated)
(8,426 missing values generated)
(8,093 missing values generated)

. 
. * keep only the variables we will actually need: 
.         keep comune_id year m5stelle_S_per m5stelle_C_per pd_S_per pd_C_per pdl_C_per pdl_S_per lega_C_per lega_S_per turnout

. 
. * reshape the data in wide format: 
.         /*
>         REMEMVER: we analyze a cross section (referendum result)
>         Thus, we do not need a tscs data but a single cs!
>         */
.         reshape wide m5stelle_S_per m5stelle_C_per pd_S_per pd_C_per pdl_C_per pdl_S_per lega_C_per lega_S_per turnout, i(comune_i
> d) j(year)
(j = 2013 2018)

Data                               Long   ->   Wide
-----------------------------------------------------------------------------
Number of observations           16,111   ->   8,337       
Number of variables                  11   ->   19          
j variable (2 values)              year   ->   (dropped)
xij variables:
                         m5stelle_S_per   ->   m5stelle_S_per2013 m5stelle_S_per2018
                         m5stelle_C_per   ->   m5stelle_C_per2013 m5stelle_C_per2018
                               pd_S_per   ->   pd_S_per2013 pd_S_per2018
                               pd_C_per   ->   pd_C_per2013 pd_C_per2018
                              pdl_C_per   ->   pdl_C_per2013 pdl_C_per2018
                              pdl_S_per   ->   pdl_S_per2013 pdl_S_per2018
                             lega_C_per   ->   lega_C_per2013 lega_C_per2018
                             lega_S_per   ->   lega_S_per2013 lega_S_per2018
                                turnout   ->   turnout2013 turnout2018
-----------------------------------------------------------------------------

. 
. * rename all variables into meaningful names:
.         rename m5stelle_C_per2013 m5s_c_13

.         rename m5stelle_S_per2013 m5s_s_13

.         rename m5stelle_C_per2018 m5s_c_18

.         rename m5stelle_S_per2018 m5s_s_18

.         
.         rename pd_C_per2013 pd_c_13

.         rename pd_S_per2013 pd_s_13

.         rename pd_C_per2018 pd_c_18

.         rename pd_S_per2018 pd_s_18

.         
.         rename pdl_C_per2013 pdl_c_13

.         rename pdl_S_per2013 pdl_s_13

.         rename pdl_C_per2018 pdl_c_18

.         rename pdl_S_per2018 pdl_s_18

.         
.         rename lega_C_per2013 lega_c_13

.         rename lega_S_per2013 lega_s_13

.         rename lega_C_per2018 lega_c_18

.         rename lega_S_per2018 lega_s_18

. 
.         rename turnout2013 turnout_13

.         rename turnout2018 turnout_18

. 
. * and label them:
.         lab var m5s_c_13 "M5S: \% votes 2013"

.         lab var m5s_s_13 "M5S: \% votes 2013"

.         lab var m5s_c_18 "M5S: \% votes 2018"

.         lab var m5s_s_18 "M5S: \% votes 2018"

.         
.         lab var pd_c_13 "PD: \% votes 2013"

.         lab var pd_s_13 "PD: \% votes 2013"

.         lab var pd_c_18 "PD: \% votes 2018"

.         lab var pd_s_18 "PD: \% votes 2018"

.         
.         lab var pdl_c_13 "PdL: \% votes 2013"

.         lab var pdl_s_13 "PdL: \% votes 2013"

.         lab var pdl_c_18 "PdL: \% votes 2018"

.         lab var pdl_s_18 "PdL: \% votes 2018"

.         
.         lab var lega_c_13 "Lega: \% votes 2013"

.         lab var lega_s_13 "Lega: \% votes 2013"

.         lab var lega_c_18 "Lega: \% votes 2018"

.         lab var lega_s_18 "Lega: \% votes 2018"

.         
.         lab var turnout_13 "\% turnout 2013"

.         lab var turnout_18 "\% turnout 2018"

.         
. * save into contemporary file:
.         tempfile KB_itcomuneelection

.         save `KB_itcomuneelection'
file /var/folders/_2/rcg0d0g53jg5kxsfmp92khb99pbw__/T//S_88530.000005 saved as .dta format

. 
. *******************************
. ******population 2016 data*****
. *******************************
. 
. /*
> Here we open the population files retrieved from the ministry. 
> Do some cleanings before merge:
> */
.         project, original("$data_original/comune info/area.csv")
project PlaceBased_analysis > do-file uses original: "data_original/comune info/area.csv" filesig(1081956119:323876)

. 
.         import delimited "$data_original/comune info/area.csv", clear
(4 vars, 7,998 obs)

. 
. * rename variables such that merge possible:    
.         rename pro_com comune_id

. 
. * create population density:    
.         gen comune_area_2016= shape_area/1000000

.         
. * keep only the infos we need: 
.         keep comune_id comune_area_2016

. 
. * safe temporary before merge: 
.         tempfile KB_comune_2016

.         save `KB_comune_2016'
file /var/folders/_2/rcg0d0g53jg5kxsfmp92khb99pbw__/T//S_88530.000006 saved as .dta format

. 
. *******************************
. ******population 2001 data*****
. *******************************
. 
.         **some covariates on comunes:
.         project, original("$data_original/comune info/Elenco_comuni_italiani_1_gennaio_2009.xls")
project PlaceBased_analysis > do-file uses original: "data_original/comune info/Elenco_comuni_italiani_1_gennaio_2009.xls" filesig(2
> 036190065:2410496)

. 
.         import excel "$data_original/comune info/Elenco_comuni_italiani_1_gennaio_2009.xls", sheet("COMUNI 01_01_2009") firstrow c
> lear 
(20 vars, 8,100 obs)

.         rename E comune_id

.         
.         gen population_2001=Popolazionelegale200121102

.         gen comune_coast=Comunelitoraneo

.         gen comune_mount=ComuneMontano

.         gen comune_area=Superficieterritorialetotale

.         gen comune_elev=Altitudinedelcentrometri

.         gen comune_density=Popolazionelegale200121102/Superficieterritorialetotale

.         
.         keep comune_id population_2001 comune_*

.         
.         tempfile cov_2001

.         save `cov_2001' 
file /var/folders/_2/rcg0d0g53jg5kxsfmp92khb99pbw__/T//S_88530.000007 saved as .dta format

. 
. 
. *******************************
. ******m5s meetup data**********
. *******************************
. /*
> Here we open the m5s meetup data we webscraped.
> Do some cleanings before merge:
> */
.         project, original("$data_coded/events_comune_neigh_nga.csv")
project PlaceBased_analysis > do-file uses original: "data_coded/events_comune_neigh_nga.csv" filesig(2414589165:3213673)

. 
.         import delimited "$data_coded/events_comune_neigh_nga.csv", clear 
(177 vars, 7,998 obs)

. 
. * data reports missings if 0, so replace: 
.         foreach w of varlist n_total-hist_wn_late {
  2.                 replace `w'=0 if `w'==.
  3.         }
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)
(7,213 real changes made)

. 
. * Create a variable measuring all M5S events AFTER referendum (placebo): 
.         gen h1=first_event

.         drop first_event

.         gen first_event = date(h1, "YMD")
(7,213 missing values generated)

.         format first_event %td

.         drop h1 

.         gen postdate=mdy(12,04,2016)

.         replace postdate=. if first_event==.
(7,213 real changes made, 7,213 to missing)

. 
.         gen placebo=0

.         replace placebo=1 if first_event>postdate
(86 real changes made)

. 
.         gen placebo_treat=placebo*n_posttreat

.         gen placebo_wn_treat=placebo*wn_posttreat

. 
. * some date cleanings:
.         replace hist_days=round(hist_days, 0.1)
(699 real changes made)

.         replace hist_days=0 if hist_days==.
(7,213 real changes made)

. 
. * check if duplicates exist: 
.         duplicates report comune_id

Duplicates in terms of comune_id

--------------------------------------
   Copies | Observations       Surplus
----------+---------------------------
        1 |         7998             0
--------------------------------------

.         // 7998 unique comune
. 
. * save as temporary file: 
.         tempfile meetup

.         save `meetup'
file /var/folders/_2/rcg0d0g53jg5kxsfmp92khb99pbw__/T//S_88530.000008 saved as .dta format

. 
. *******************************
. ******Campante Internet data***
. *******************************
. /*
> Our instrumental variable approach is based on: 
> Campante, Filipe, Ruben Durante, and Francesco Sobbrio. 2018. “Politics 2.0: The Multifaceted Effect
> of Broadband Internet on Political Participation.” Journal of the European Economic Association 16
> (4): 1094–1136.
> 
> They shared their data with us, which we then merge here with our data.
> */
.         project, original("$data_original/Campanteetal/ugs_politics_2.0.dta")
project PlaceBased_analysis > do-file uses original: "data_original/Campanteetal/ugs_politics_2.0.dta" filesig(4238880732:136087)

. 
.         use "$data_original/Campanteetal/ugs_politics_2.0.dta", clear 

.         rename istat_code_110 comune_id

.         rename distance_closest_SGU km_to_ugs

. 
.         tempfile Campanteetal

.         save `Campanteetal'
file /var/folders/_2/rcg0d0g53jg5kxsfmp92khb99pbw__/T//S_88530.000009 saved as .dta format

. 
. *******************************
. ******Schaub/Morisi Internet data***
. *******************************
. /*
> Our instrumental variable approach is based on: 
> Schaub, Max, and Davide Morisi. 2020. “Voter Mobilisation in the Echo Chamber: Broadband Internet
> and the Rise of Populism in Europe.” European Journal of Political Research
> 
> Replication file downloaded:
> https://ejpr.onlinelibrary.wiley.com/action/downloadSupplement?doi=10.1111%2F1475-6765.12373&file=ejpr12373-sup-0002-Replicationda
> ta.zip
> */
.         project, original("$data_original/SchaubMorisi/Italy/italy_broadband.dta")
project PlaceBased_analysis > do-file uses original: "data_original/SchaubMorisi/Italy/italy_broadband.dta" filesig(1828491743:32667
> 5)

. 
.         use "$data_original/SchaubMorisi/Italy/italy_broadband.dta", clear

.         
.         *rename comune ID variable to match ours:       
.         rename id_comunenew2 comune_id

. 
.         tempfile SchaubMorisi

.         save `SchaubMorisi'
file /var/folders/_2/rcg0d0g53jg5kxsfmp92khb99pbw__/T//S_88530.00000a saved as .dta format

. 
. ******************************************************
. ********USING REGIONAL DATA AND MERGE WITH M5S********
. ******************************************************
.         project, original("$data_original/referendum results/referendum.dta")
project PlaceBased_analysis > do-file uses original: "data_original/referendum results/referendum.dta" filesig(3849762814:5958500)

. 
.         use "$data_original/referendum results/referendum.dta", clear 

. 
.         * we do some renaming in order to prepare merges 
.         rename istatmunicipalitycode comune_id

.         drop if comune_id==.
(150 observations deleted)

. 
.         * then we merge with our M5S event data: 
.         merge 1:1 comune_id using `meetup'

    Result                      Number of obs
    -----------------------------------------
    Not matched                             1
        from master                         1  (_merge==1)
        from using                          0  (_merge==2)

    Matched                             7,998  (_merge==3)
    -----------------------------------------

.         drop if _merge==1
(1 observation deleted)

.         drop _merge

. 
.         * then we merge with 2016 area information: 
.         merge 1:1 comune_id using `KB_comune_2016'

    Result                      Number of obs
    -----------------------------------------
    Not matched                             0
    Matched                             7,998  (_merge==3)
    -----------------------------------------

.         drop if _merge==1
(0 observations deleted)

.         drop _merge

. 
.         * then merge with 2001 population infos: 
.         merge 1:1 comune_id using `cov_2001' 

    Result                      Number of obs
    -----------------------------------------
    Not matched                           450
        from master                       174  (_merge==1)
        from using                        276  (_merge==2)

    Matched                             7,824  (_merge==3)
    -----------------------------------------

.         drop if _merge==2
(276 observations deleted)

.         drop _merge

. 
. * one comune is missing!
. * we code the information from infos from the ministry of interior:
. * https://elezionistorico.interno.gov.it/index.php?tpel=F&dtel=04/12/2016&tpa=I&tpe=C&lev0=0&levsut0=0&lev1=3&levsut1=1&lev2=15&le
> vsut2=2&levsut3=3&ne1=3&ne2=15&es0=S&es1=S&es2=S&es3=N&ms=S&ne3=151790&lev3=1790
.         replace nvoters=1085 if comune_id==17189
(1 real change made)

.         replace nconstituents=1076 if comune_id==17189
(1 real change made)

.         replace referendumyes=458 if comune_id==17189
(1 real change made)

.         replace referendumno=618 if comune_id==17189
(1 real change made)

. 
. *we clean the regional level controls:
.         **referendum voting:
.         gen turnout=(nvoters/nconstituents)*100

.         gen referendum_yes=(referendumyes/nvoters)*100
(4 missing values generated)

.         gen referendum_no=(referendumno/nvoters)*100
(4 missing values generated)

.         drop referendumyes referendumno

. 
.         **foreigners 
.         gen foreigners=(foreigners_total/comune_population)*100
(33 missing values generated)

.         
.         **education 
.         replace university="." if university==".."
(3 real changes made)

.         destring university, replace
university: all characters numeric; replaced as long
(59 missing values generated)

.         rename university h1 

.         gen university=(h1/comune_population)*100
(59 missing values generated)

.         drop h1 

.         
.         foreach var of varlist literate_no_formal_educ_65 literate_no_formal_educ {
  2.                 replace `var'="." if `var'==".."
  3.                 destring `var', replace
  4.         }
(110 real changes made)
literate_no_formal_educ_65: all characters numeric; replaced as long
(166 missing values generated)
(9 real changes made)
literate_no_formal_educ: all characters numeric; replaced as long
(65 missing values generated)

.         
.         gen no_edu=((literate_no_formal_educ_65+literate_no_formal_educ+primary_school)/comune_population)*100
(166 missing values generated)

.         
.         **unemployment 
.         replace unemployment="." if unemployment==".."
(28 real changes made)

.         destring unemployment, replace
unemployment: all characters numeric; replaced as double
(85 missing values generated)

.         
.         **gdp 
.         gen income=taxableincome/comune_population

.         gen log_income=log(taxableincome)

.         
.         **population 
.         gen log_pop=log(comune_population)

.         
.         **urbanity from pop: 
.         gen urbanity=0 

.         replace urbanity=1 if comune_population>=5000 & comune_population<10000
(1,186 real changes made)

.         replace urbanity=2 if comune_population>=10000 & comune_population<30000
(913 real changes made)

.         replace urbanity=3 if comune_population>=30000 & comune_population<100000
(265 real changes made)

.         replace urbanity=4 if comune_population>=100000 & comune_population<250000
(34 real changes made)

.         replace urbanity=5 if comune_population>=250000 
(12 real changes made)

.         
.         **encoding of regional identifiers:
.         drop region_id

.         encode region, gen(region_id)

. 
.         ** some identifiers are missing.
.         ** Thus, we drop the variable and clean by hand:
.         drop province_id

. 
.         **some recodes needed in order to get province for each comune (done by hand):
.         replace province="Medio Campidano" if comune=="Arbus"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Barumini"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Buggerru"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Calasetta"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Carbonia"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Carloforte"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Collinas"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Domusnovas"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Fluminimaggiore"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Furtei"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Genuri"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Gesturi"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Giba"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Gonnesa"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Gonnosfanadiga"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Guspini"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Iglesias"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Las Plassas"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Lunamatrona"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Masainas"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Musei"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Narcao"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Nuxis"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Pabillonis"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Pauli Arbarei"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Perdaxius"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Piscinas"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Portoscuso"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Samassi"
(1 real change made)

.         replace province="Medio Campidano" if comune=="San Gavino Monreale"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="San Giovanni Suergiu"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Sanluri"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Sant'Anna Arresi"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Sant'Antioco"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Santadi"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Sardara"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Segariu"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Serramanna"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Serrenti"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Setzu"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Siddi"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Tratalias"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Tuili"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Turri"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Ussaramanna"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Villacidro"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Villamar"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Villamassargia"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Villanovaforru"
(1 real change made)

.         replace province="Medio Campidano" if comune=="Villanovafranca"
(1 real change made)

.         replace province="Carbonia-Iglesias" if comune=="Villaperuccio"
(1 real change made)

.         
.         **encode it: 
.         encode province, gen(province_id)

. 
.         ** single ID for each case:
.         drop id 

.         gen id=_n

. 
.         *we standardize the exposure variables as discussed in the paper:
.         gen exposure_pop=nvoters/1000

. 
.         foreach var of varlist placebo_* n_total-hist_wn_late {
  2.                 gen std_`var'=log((`var'/exposure_pop)+1)
  3.         }
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)
(4 missing values generated)

. 
.         **creating a binary exposure indicator: 
.         gen m5s_ever=0

.         replace m5s_ever=1 if n_total>=1
(785 real changes made)

.         
.         **creating dummy for early M5S:
.         gen early_dummy=0

.         replace early_dummy=1 if hist_n_early>0
(98 real changes made)

.         
.         **creating a binary exposure indicator for M5S during referendum: 
.         gen m5s_ref_ever=0

.         replace m5s_ref_ever=1 if n_treat_campaign>0
(622 real changes made)

.         
.         **creating a binary exposure indicator for M5S during short referendum period: 
.         gen m5s_ref_ever_short=0

.         replace m5s_ref_ever_short=1 if n_treat_campaign_short>0
(552 real changes made)

.         
.         
.         **creating a binary exposure indicator for adjacent M5S during referendum: 
.         gen m5s_adjacent_ever=0

.         replace m5s_adjacent_ever=1 if n_neigh_treat_campaign>0
(2,745 real changes made)

.         
. * Now we merge this final data the cleaned data we proceed above:
.         
.         * cross section election results: 
.         merge 1:1 comune_id using `KB_itcomuneelection'
(variable comune_id was long, now double to accommodate using data's values)

    Result                      Number of obs
    -----------------------------------------
    Not matched                           339
        from master                         0  (_merge==1)
        from using                        339  (_merge==2)

    Matched                             7,998  (_merge==3)
    -----------------------------------------

.         drop if _merge==2
(339 observations deleted)

.         drop _merge

. 
.         * replication data from Campanteetal:
.         merge m:m comune_id using `Campanteetal'

    Result                      Number of obs
    -----------------------------------------
    Not matched                           213
        from master                        60  (_merge==1)
        from using                        153  (_merge==2)

    Matched                             7,938  (_merge==3)
    -----------------------------------------

.         drop if _merge==2
(153 observations deleted)

.         drop _merge

. 
.         * replication data from Schaub and Morisi (for instrument validation):
.         merge 1:1 comune_id using `SchaubMorisi'

    Result                      Number of obs
    -----------------------------------------
    Not matched                           133
        from master                        37  (_merge==1)
        from using                         96  (_merge==2)

    Matched                             7,961  (_merge==3)
    -----------------------------------------

.         drop if _merge==2
(96 observations deleted)

.         drop _merge

. 
.         * clean internet: 
.         foreach var of varlist adsl* {
  2.                 replace `var'=(1-`var')*100
  3.         }
(7,961 real changes made)
(7,961 real changes made)
(7,961 real changes made)
(7,961 real changes made)

.         gen adsl18=nga_coverage
(328 missing values generated)

.         
.         lab var adsl12 "\% population with broadband internet in 2012"

.         lab var adsl13 "\% population with broadband internet in 2013"

.         lab var adsl14 "\% population with broadband internet in 2014"

.         lab var adsl15 "\% population with broadband internet in 2015"

.         lab var adsl18 "\% population with broadband internet in 2018"

.         
.         * produce log of days + km to UGS as outlined in paper: 
.         gen log_hist=log(hist_days+1)

.         gen log_ugs=log(km_to_ugs+1)
(60 missing values generated)

. 
.         *population density: 
.         gen pop_density_16=comune_population/comune_area_2016

.         gen pop_density_01=population_2001/comune_area
(174 missing values generated)

.         
.         **labeling variables 
.         lab var pop_density_16 "population density 2016"

.         lab var comune_area_2016 "area 2016"

.         lab var population_2001 "Population in 2001"

.         lab var hist_days "days since formation"

.         lab var log_hist "log days since formation"

.         lab var log_ugs "log distance to closest UGS (in km)"

.         lab var referendum_no "\% No in referendum"

.         lab var std_wn_treat_campaign "M5S activity (cont.)"

.         lab var std_wn_neigh_treat_campaign "M5S adjacent activity (cont.)"

.         lab var std_n_treat_campaign "M5S: referendum {it:(events only)}"

.         lab var std_hist_wn_early "M5S: phase I"

.         lab var std_hist_wn_mid "M5S: phase II"

.         lab var std_hist_n_early "M5S: phase I {it:(events only)}"

.         lab var std_hist_n_mid "M5S: phase II {it:(events only)}"

.         lab var urbanity "urbanity"

.         label define urbanity 0 "<5 000" 1 "<10 000" 2 "<30 000" 3 "<100 000" 4 "<250 000" 5 "$\geq$ 250 000"

.         label values urbanity urbanity

.         lab var std_n_total "M5S: since 2005"

.         lab var std_n_treat_24m "M5S: 24 months before referendum"

.         lab var std_n_treat_12m "M5S: 12 months before referendum"

.         lab var std_n_treat_6m "M5S: 6 months before referendum"

.         lab var std_n_treat_90d "M5S: 3 months before referendum"

.         lab var std_n_treat_60d "M5S: 2 months before referendum"

.         lab var std_n_treat_30d "M5S: 1 month before referendum"

.         lab var foreigners "\% foreigners"

.         lab var log_pop "population (logged)"

.         lab var no_edu "\% low education"

.         lab var university "\% university degree"

.         lab var unemployment "\% unemployed"

.         lab var log_income "income (logged)"

.         lab var income "income per cap"

.         lab var nga_coverage "internet: coverage (0-1)"

.         lab var nga_dummy "internet: coverage (0,1)"

.         lab var std_placebo_treat "M5S: placebo"

.         lab var m5s_ref_ever "M5S activity (0,1)"

.         lab var m5s_adjacent_ever "M5S adjacent: binary"

.         lab var m5s_ref_ever_short "M5S activity (0,1)"

.         lab var std_wn_treat_campaign_short "M5S activity (cont.)"

.         lab var std_wn_posttreat "M5S post referendum"

.         lab var std_wn_treat_loc_indoor "M5S indoor"

.         lab var std_wn_treat_loc_outdoor "M5S outdoor"

. 
.         **final cleanings and ordering       
.         order id comune comune_id province* region* turnout referendum_yes referendum_no

.         
.         
.         **save and be done:
.         save "$data_coded/placebased_regional.dta", replace 
file /Users/au660280/Dropbox/m5S/JoP_PlaceBasedCampaigning_replication/data_coded/placebased_regional.dta saved

. 
.         *codebookout "$data_coded/codebookplacebased_regional", replace
.         
.         project, creates("$data_coded/placebased_regional.dta")
project PlaceBased_analysis > do-file creates: "data_coded/placebased_regional.dta" filesig(1029121884:16892740)

.         
. 
end of do-file
      name:  plog_2
       log:  /Users/au660280/Dropbox/m5S/JoP_PlaceBasedCampaigning_replication/code/do/05_prepare_regional.log
  log type:  text
 closed on:  19 Sep 2022, 11:38:44
------------------------------------------------------------------------------------------------------------------------------------
